<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="仓位名称" prop="equipmentName">
        <el-input v-model="queryParams.storeLocationName" placeholder="请输入仓位名称" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="仓位编码" prop="equipmentCode">
        <el-input v-model="queryParams.storeLocationCode" placeholder="请输入仓位编码" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="设备名称" prop="equipmentName">
        <el-input v-model="queryParams.equipmentName" placeholder="请输入监控设备名称" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="设备编码" prop="equipmentCode">
        <el-input v-model="queryParams.equipmentCode" placeholder="请输入监控设备编码" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="预警类型" prop="warningType" width="120">
        <template slot-scope="scope">
          <el-select v-model="queryParams.warningType" placeholder="手动巡检预警" :onSelectionChange="handleQuery" clearable>
            <el-option v-for="dict in dict.type.platform_warning_type" :key="dict.value" :label="dict.label"
                       :value="dict.value" />
          </el-select>
        </template>
      </el-form-item>
      <el-form-item label="预警时间" prop="warningTimeeRange">
        <div class="block">
          <el-date-picker v-model="queryParams.warningTimeRange" type="datetimerange"
            format="yyyy-MM-dd HH:mm:ss" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期"
            align="right">
          </el-date-picker>
        </div>
      </el-form-item>
      <el-form-item >
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['platform:warning:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="warningList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="系统编号" align="center" prop="warningId" />
      <el-table-column label="监控设备名称"  width="100" align="center" prop="equipmentName" :show-overflow-tooltip="true"/>
      <el-table-column label="监控设备编码"  width="100" align="center" prop="equipmentCode" :show-overflow-tooltip="true"/>
      <el-table-column label="仓位名称" align="center" prop="storeLocationCode" :show-overflow-tooltip="true"/>
      <el-table-column label="仓位编码" align="center" prop="storeLocationName" :show-overflow-tooltip="true"/>
      <el-table-column label="仓位预置点位"  width="100" align="center" prop="presetId" />
      <el-table-column label="预警类型" width="120" align="center" prop="warningType" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.platform_warning_type" :value="scope.row.warningType" />
        </template>
      </el-table-column>
      <!-- <el-table-column label="预警内容" align="center" prop="warningContent" /> -->
      <el-table-column label="预警时间" align="center" prop="warningTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.warningTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
            v-hasPermi="['platform:warning:edit']"
          >查看</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改【预警信息】对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="监控设备ID" prop="equipmentId">
          <el-input v-model="form.equipmentId" placeholder="请输入监控设备ID" />
        </el-form-item>
        <el-form-item label="仓位ID" prop="storeLocationId">
          <el-input v-model="form.storeLocationId" placeholder="请输入仓位ID" />
        </el-form-item>
        <el-form-item label="仓位编码" prop="storeLocationCode">
          <el-input v-model="form.storeLocationCode" placeholder="请输入仓位编码" />
        </el-form-item>
        <el-form-item label="预警内容">
          <editor v-model="form.warningContent" :min-height="192"/>
        </el-form-item>
        <el-form-item label="预警时间" prop="warningTime">
          <el-date-picker clearable
            v-model="form.warningTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择预警时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="创建时间" prop="createDate">
          <el-date-picker clearable
            v-model="form.createDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择创建时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <!-- <el-button type="primary" @click="submitForm">确 定</el-button> -->
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!--查看详情对话框 -->
    <el-dialog title="查看预警详情" :close="cancelDteail" :visible.sync="viewOpen" width="800px" append-to-body>
      <div>
        <el-form label-width="120px" v-if="viewOpen">
          <el-row>
            <el-col :span="12">
              <el-form-item label="设备名称：" :show-overflow-tooltip="true">
                <el-text class="mx-1" size="large"> {{ this.viewData.equipmentName }}</el-text>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="设备编码：" prop="equipmentChannel" :show-overflow-tooltip="true">
                <el-text class="mx-1" size="large"> {{ this.viewData.equipmentCode }}</el-text>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="仓位名称：" :show-overflow-tooltip="true">
                <el-text class="mx-1" size="large"> {{ this.viewData.storeLocationCode }}</el-text>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="仓位编码："  :show-overflow-tooltip="true">
                <el-text class="mx-1" size="large"> {{ this.viewData.storeLocationName }}</el-text>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="预置点位ID：" :show-overflow-tooltip="true">
                <el-text class="mx-1" size="large"> {{ this.viewData.presetId }}</el-text>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="预警状态：" :show-overflow-tooltip="true">
                <dict-tag :options="dict.type.platform_warning_type"
                  :value="this.viewData.warningType" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-form-item label="预警内容：">
            <el-input v-model="this.viewData.warningContent" type="textarea" placeholder="预警内容" disabled />
          </el-form-item>
          <el-row>
            <el-col :span="12">
              <el-form-item label="预警时间：" :show-overflow-tooltip="true">
                {{ parseTime(this.viewData.warningTime, '{y}-{m}-{d} {h}:{i}:{s}')
                }}
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="创建时间：" :show-overflow-tooltip="true">
                {{ parseTime(this.viewData.createDate, '{y}-{m}-{d} {h}:{i}:{s}')
                }}
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer" v-if="viewOpen">
        <el-button @click="cancelDteail" icon="el-icon-back">返 回</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listWarning } from "@/api/platform/warning"

export default {
  name: "Warning",
  dicts:['platform_warning_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      viewOpen: false,
      viewData: null,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 【预警信息】表格数据
      warningList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        equipmentId: null,
        storeLocationId: null,
        storeLocationCode: null,
        warningType: null,
        warningContent: null,
        warningTime: null,
        createDate: null,
        warningTimeRange: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        equipmentId: [
          { required: true, message: "监控设备ID不能为空", trigger: "blur" }
        ],
        storeLocationId: [
          { required: true, message: "仓位ID不能为空", trigger: "blur" }
        ],
        storeLocationCode: [
          { required: true, message: "仓位编码不能为空", trigger: "blur" }
        ],
        warningTime: [
          { required: true, message: "预警时间不能为空", trigger: "blur" }
        ],
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询【预警信息】列表 */
    getList() {
      this.loading = true
      if (Array.isArray(this.queryParams.warningTimeRange)) {
        this.queryParams.warningStartTime = moment(this.queryParams.warningTimeRange[0]).format('YYYY-MM-DD HH:mm:ss')
        this.queryParams.warningEndTime = moment(this.queryParams.warningTimeRange[1]).format('YYYY-MM-DD HH:mm:ss')
      }
      listWarning(this.queryParams).then(response => {
        this.warningList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        warningId: null,
        equipmentId: null,
        storeLocationId: null,
        storeLocationCode: null,
        warningType: null,
        warningContent: null,
        warningTime: null,
        createDate: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.warningId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 查看按钮操作 */
    handleView(row) {
      this.viewOpen = true
      this.viewData=row
    },
    cancelDteail() {
       this.viewOpen = false
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('platform/warning/export', {
        ...this.queryParams
      }, `warning_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
